﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[AddComponentMenu("MyGame/Player")]
public class Player : MonoBehaviour {
    private float m_speed = 5;
    protected Transform m_transform;

    private float m_RocateRate;
    float m_rocketRate = 0;
    //声音
    public AudioClip m_shootClip;
    //声音源
    protected AudioSource m_audio;
    //爆炸特效
    public Transform m_explosionFX;

    public Transform m_rocket;
    //生命值
    public int m_life = 5;
    //增加子弹
    protected int Rocketbuff=1;
    //子弹buff时间
    public float buff_time = 0;
    //子弹速度buff时间
    public float speed_time = 0;


    private void Awake()
    {
      m_transform = this.transform;
     

        m_audio = this.m_transform.GetComponent<AudioSource>();
    }
	// Use this for initialization
    
	void Start () {

        

    }
	
	// Update is called once per frame
	void Update () {
        Rocket rocket = m_rocket.GetComponent<Rocket>();
        speed_time -= Time.deltaTime;
        if (speed_time > 0)
        {
            rocket.m_speed = 20;
        }
        else
        {
            rocket.m_speed = 10;

        }
        if (m_life<=0)
        {
            //爆炸特效
            Instantiate(m_explosionFX, m_transform.position, Quaternion.identity);
            Destroy(this.gameObject);

        }
        m_RocateRate -= Time.deltaTime;

        float move_h = Input.GetAxisRaw("Horizontal");
        float move_v = Input.GetAxisRaw("Vertical");

        if (move_h!=0||move_v!=0)
        {
            m_transform.Translate(new Vector3(-move_h,0,-move_v)*m_speed*Time.deltaTime);
        }
        buff_time -= Time.deltaTime;
        m_rocketRate -= Time.deltaTime;
        if (m_rocketRate<=0)
        {
            m_rocketRate = 0.1f;
          
            if (buff_time <= 0)
            {
                Rocketbuff = 1;
                buff_time = 0;
            }
            if (Input.GetKey(KeyCode.Space)||Input.GetMouseButton(0))
        {
              
                if (Rocketbuff==1)
                {
                    Instantiate(m_rocket, m_transform.position, m_transform.rotation);

                }
                if (Rocketbuff == 2 && buff_time > 0)
                {
                    Vector3 relativePos2 = m_transform.position +new Vector3(0.5f,0,0);
                    Vector3 relativePos1 = m_transform.position - new Vector3(0.5f, 0, 0);
                    Instantiate(m_rocket, relativePos1, m_transform.rotation);
                    Instantiate(m_rocket, relativePos2, m_transform.rotation);
                }
                if (Rocketbuff == 3 && buff_time > 0)
                {
                    Vector3 relativePos2 = m_transform.position + new Vector3(0.5f, 0, 0);
                    Vector3 relativePos1 = m_transform.position - new Vector3(0.5f, 0, 0);
                    Instantiate(m_rocket, relativePos1, m_transform.rotation);
                    Instantiate(m_rocket, m_transform.position, m_transform.rotation);

                    Instantiate(m_rocket, relativePos2, m_transform.rotation);
                }
                if (Rocketbuff == 4 && buff_time > 0)
                {
                    Vector3 relativePos2 = m_transform.position + new Vector3(0.5f, 0, 0);
                    Vector3 relativePos1 = m_transform.position - new Vector3(0.5f, 0, 0);
                    Vector3 relativePos3 = m_transform.position + new Vector3(1f, 0, 0);
                    Vector3 relativePos4 = m_transform.position - new Vector3(1f, 0, 0);
                    Instantiate(m_rocket, relativePos1, m_transform.rotation);
                    Instantiate(m_rocket, relativePos2, m_transform.rotation);
                    Instantiate(m_rocket, relativePos3, m_transform.rotation);
                    Instantiate(m_rocket, relativePos4, m_transform.rotation);
                }


                //播放射击声音
                m_audio.PlayOneShot(m_shootClip);
            }

        }
     

    }

    //碰撞
    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "EnemyRocket"|| other.tag == "Enemy")
        {
            Rocket Enemy_rocket = other.GetComponent<EnemyRocket>();

             m_life--;
           
        }
        if (other.tag == "lifebuff")
        {
            if (m_life<5)
            {
             m_life++;
            }
            else
            {
                m_life = 5;
            }

        }
        if (other.tag == "Rocketbuff")
        {
            if (Rocketbuff<4)
            {
               Rocketbuff++;
            }
            else
            {
                Rocketbuff = 4;
            }
            buff_time = 4;

        }
        if (other.tag == "Speedbuff")
        {

            speed_time = 3;

        }
          

    }

   
}
